home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7593 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.ov.com!news
  2. From: glenn@ov.com (Fletcher.Glenn@ov.com)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Question - C (beginner)
  5. Date: 27 Feb 1996 16:32:48 GMT
  6. Organization: OpenVision
  7. Message-ID: <4gvbng$tf@spanky.pls.ov.com>
  8. References: <312FBA11.3946@netgate.net>
  9. Reply-To: glenn@ov.com
  10. NNTP-Posting-Host: foghorn.pls.ov.com
  11.  
  12. In article 3946@netgate.net, Tamara Johnson <malihini@netgate.net> writes:
  13. >Problem: not correctly counting chars in
  14. >each word, not correctly counting words
  15. >with >0 && <7 chars and words with >6 chars.
  16. >
  17. >Suggestions greatly appreciated,
  18. >Tamara
  19. >[stuff deleted - snippet?]
  20. >{
  21. >int i, cc, nl, nw, nc,inword;
  22. >
  23. >inword=NO;
  24. >cc=nl=nw=nc=0;
  25. >
  26. >  for(i=0;i<n;i++)
  27. >    {
  28. >    nc++;
  29. >    cc++;
  30.  
  31. Above, you are counting a character before you decide that it is
  32. part of the word.  To correct this your second "if" below should
  33. do cc-- to uncount the character that is not part of the word.
  34.  
  35.         Fletcher.Glenn@ov.com
  36.  
  37. >    if(test_string[i]=='\n')
  38. >      nl++;
  39. >    if(test_string[i]==' '||test_string[i]=='\n'
  40. >       ||test_string[i]=='\0'||test_string[i]=='\t')
  41. >      inword=NO;
  42. >    else if(inword==NO)
  43. >      {
  44. >      inword=YES;
  45. >      nw++;
  46. >      if((cc>0)&&(cc<7)) under++;
  47. >      if(cc>6) over++;
  48. >      cc=0;
  49. >      }
  50. >    }
  51. >}
  52. >
  53. >/* -----output------- 
  54. >
  55. >Enter test string: tickle something ugly
  56. >Number of words in the string: 3
  57. >Number of words with 7 or more char: 2
  58. >Number of words with 6 or less char: 1
  59. >*/
  60. >-- 
  61. >/* malihini@netgate.net             */
  62. >/* a.k.a. johnson_tamara@tandem.com */
  63. >/* I used to think I knew logic,    */
  64. >/* then I found C.                  */
  65.  
  66.  
  67.  
  68.  
  69.  
  70.